package com.smrtcold.dev.controller;


import com.smrtcold.dev.init.SystemInit;
import com.smrtcold.dev.mapper.DataMapper;
import com.smrtcold.dev.utils.R;
import com.smrtcold.dev.utils.Utils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("api/sys")
public class SysController {


    @Value("${sys.password}")
    private String syspassword;

    @Resource
    private SystemInit systemInit;
    @Resource
    private DataMapper dataMapper;


    @GetMapping("/mid/{token}")
    public Object getMid(@PathVariable String token) {
        return R.put(systemInit.getMid());
    }

    /**
     * 获取系统状态
     *
     * @return
     */
    @GetMapping("/status/{token}")
    public Object status(@PathVariable String token) {
        return Utils.verAck(token) ? R.put(systemInit.getISRUN()) : R.err("token错误或过期!");
    }

    @GetMapping("/ctlcount/{token}")
    public Object ctlcount(@PathVariable String token) {
        return Utils.verAck(token) ? R.put(dataMapper.ctlcount()) : R.err("token错误或过期!");
    }

    @GetMapping("/runtsk/{token}/{taskid}")
    public Object ctlcount(@PathVariable String token, @PathVariable Long taskid) {
        try {
            if (!Utils.verAck(token)) {
                return R.err("token错误或过期!");
            }
            systemInit.runTask(taskid);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.err();
    }

    @GetMapping("/add")
    public Object ctlcount(@RequestParam String token, String vck, @RequestParam String devid, @RequestParam String addres, @RequestParam Number value) {
        if (!token.equals(syspassword) || !("H" + LocalDateTime.now().getHour()).equals(vck)) return R.err("验证失败!");
        try {
            this.systemInit.sendCtl(devid, addres, value);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.err();
    }


    /**
     * @param isRun
     * @param password
     * @return
     */
    @PostMapping("/status/{isRun}/{password}/{token}")
    public R setStatus(@PathVariable boolean isRun, @PathVariable String password, @PathVariable String token) {
        if (password.equals(syspassword) && Utils.verAck(token)) {
            try {
                systemInit.setISRUN(isRun, 0);
                return R.put(systemInit.getISRUN());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return R.err("设置失败").put("data", systemInit.getISRUN());
        }
        return R.err("密码错误!");

    }

    @PostMapping("/sisdata/{token}")
    public R setStatus(@PathVariable String token, String[] keys, @RequestParam(defaultValue = "7") int queryday) {
        if (Utils.verAck(token) && null != keys) {
            R r = R.ok();
            for (String key : keys) {
                r.put(key, dataMapper.getSisData(key, queryday));
            }
            return r;
        }
        return R.err("密码错误!");
    }


    @PostMapping("/ctlData/{token}")
    public R ctlData(@PathVariable String token, @RequestParam(defaultValue = "5") int queryday) {
        if (Utils.verAck(token)) {
            R r = R.ok();
            r.put("all", dataMapper.ctlData("status", 1, queryday));
            r.put("isctl", dataMapper.ctlData("isctl", 1, queryday));
            r.put("xx", dataMapper.ctlData("status", 0, queryday));
            return r;
        }
        return R.err("密码错误!");
    }

}
